/*
 * @Author: gyc
 * @Date: 2020-07-22 13:12:57
 * @LastEditTime: 2020-07-28 10:02:45
 * @Description: Nuxt测试版
 */

const Service = require('egg').Service;
const moment = require('moment');

class ShareService extends Service {
    //创建分享内容
    async create(data) {
        let result, mod
        const createtimes = moment().format('YYYY-MM-DD HH:mm:ss')
        const lastCreate = await this.app.mysql.select('yui_share', {
            where: {
                userid: '16'
            },
            columns: ['createTm'],
            limit: 1,
            orders: [
                ['id', 'desc']
            ]

        })
        if (lastCreate[0]) {
            const lastTm = moment(lastCreate[0].createTm);
            const auth = moment().diff(lastTm, 'minute')
            if (auth != 0) {
                mod = 1
                result = await this.app.mysql.insert("yui_share", {
                    title: data.title,
                    userid: '16',
                    kind: data.kind,
                    area: data.area,
                    tags: data.tags,
                    rate: data.rate,
                    profile: data.profile,
                    href: data.href,
                    platform: data.platform,
                    status: '0',
                    createTm: createtimes,
                    updateTm: createtimes
                })
            } else {
                mod = 2
                result = "时间未到！"
            }
        } else {
            result = await this.app.mysql.insert("yui_share", {
                title: data.title,
                userid: '16',
                kind: data.kind,
                area: data.area,
                tags: data.tags,
                rate: data.rate,
                profile: data.profile,
                href: data.href,
                platform: data.platform,
                status: '0',
                createTm: createtimes,
                updateTm: createtimes
            })
            mod = 3
        }

        return {
            result,
            mod
        };
    }
    //查询我的分享列表
    async findMy(userid, page) {
        const result = this.app.mysql.select('yui_share', {
            where: {
                userid: userid
            },
            orders: [
                ['id', 'asc']
            ],
            limit: 10,
            offset: 10 * (page - 1)
        })
        return result
    }
    //删除我的分享
    async deleteMyShare(userid, id) {
        const result = this.app.mysql.delete('yui_share', {
            userid: userid,
            id: id
        })
        return result
    }
}
module.exports = ShareService;